<?php
/**
 * 管理员
 * Created by Vscode.
 * User: Luanzg
 * Date: 2020/05/22 1526
 * Time: 15:26
 */
namespace app\common\validate;

use app\common\facade\model\AdminRuleModel;

class Rules extends BaseValidate
{

    /**
     * 权限标识唯一性验证
     * 自定义验证器
     */
    protected function pathUrlUnique($value, $rule, $data)
    {

        $where[] = ['path_url', '=', $value];

        $info = AdminRuleModel::info($where, ['id']);

        if(isset($data['id']) && !empty($info)){
          if($info->id != $data['id'])  exception('权限标识具有唯一性！');
        }else{
          if (!empty($info)) exception('权限标识具有唯一性！');
        }

        return true;
    }

    protected $field = [
        'auth_name'      => '角色名',
        'path_url'  => '权限标识'

    ];

    protected $rule = [
        'auth_name'   => 'require',
        'path_url'    => 'pathUrlUnique'
    ];

    /**
     * 登录
     */
    protected function sceneaddsave()
    {
        return $this->only([
            'auth_name',
            'path_url'
        ]);
    }

}
